//
//  MeSetInfoVC.m
//  Ginix
//
//  Created by fzq on 2025/2/17.
//

#import "MeSetInfoVC.h"
#import "MeSetInfoCell.h"
#import <BRPickerView.h>
#import "PhotoManager.h"
#import "MeSetNameView.h"

@interface MeSetInfoVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * listView;
@property (nonatomic,strong) UIButton       * enterBtn;
@property (nonatomic,strong) BRDatePickerView* dateView;
@property (nonatomic,strong) BRStringPickerView * sexView;
@property (nonatomic,strong) MeSetNameView * setNameView;
@end

@implementation MeSetInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViewLayout];
}

-(void)setSubViewLayout{
    
    self.title = @"个人信息";
    [self.view addSubview:self.enterBtn];
    [self.view addSubview:self.listView];
    
}

-(void)enterBtnAction{
    
    if ([UserInfoModel.shareInstance.userModel.birthday isEqual:@""]) {
        FhudShowStr(@"请选择生日");
        return;
    }
    if ([UserInfoModel.shareInstance.userModel.height isEqual:@""]) {
        FhudShowStr(@"请输入身高");
        return;
    }
    if ([UserInfoModel.shareInstance.userModel.weight isEqual:@""]) {
        FhudShowStr(@"请输入体重");
        return;
    }
    if ([UserInfoModel.shareInstance.userModel.nickName isEqual:@""]) {
        FhudShowStr(@"请输入昵称");
        return;
    }
    if (UserInfoModel.shareInstance.userModel.sex == 0) {
        FhudShowStr(@"请选择性别");
        return;
    }
   
    UserInfoModel * model = UserInfoModel.shareInstance.userModel;
    
    NSDictionary * parm = @{@"sex":@(model.sex),@"birthday":model.birthday,@"height":model.height,@"weight":model.weight,@"nickName":model.nickName};
    [SPNetworkingIns POST:PORT(@"user-center/api/appUser/updateAppUserInfo") params:parm success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        FhudShowStr(@"修改成功");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 7;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        
    if(indexPath.row == 0 ){
        return 70 * FMScaleX;
    }else{
        return  56 * FMScaleX;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MeSetInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MeSetInfoCell"];
    [cell setIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.row == 4){
        [self.dateView show];
    }
    if (indexPath.row == 0) {
        [self updateHeadImage];
    }
    if (indexPath.row == 3) {
        [self.sexView show];
    }
    if (indexPath.row == 1) {
        [self.setNameView startAnimationWithType:1];
    }
    if (indexPath.row == 5) {
        [self.setNameView startAnimationWithType:2];
    }
    if (indexPath.row == 6) {
        [self.setNameView startAnimationWithType:3];
    }
}

-(void)updateHeadImage{
    
    [[PhotoManager sharedInstance] takePhoto:self finished:^(HXPhotoModel * _Nonnull model) {
        @WeakObj(self)
        FLoding
        [SPNetworkingIns updateIdImage:model.previewPhoto url:PORT(@"user-center/api/appUser/updateAppUserAvatar") name:@"avatarFile" success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
            FHideLoding
            @StrongObj(self)
            NSString * imagStr = K_TimeJson(responseObject, @"data");
            UserInfoModel.shareInstance.userModel.avatar = imagStr;
            [self.listView reloadData];
        } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
        }];
    }];
    
}

-(UIButton *)enterBtn{
    
    if (!_enterBtn) {
        _enterBtn = [[UIButton alloc]initWithFrame:CGRectMake(30 * FMScaleX, HEIGHT - NavHeight - kSafeAreaHeight - 70 * FMScaleX, WIDTH - 60 * FMScaleX, 50 * FMScaleX)];
        [_enterBtn setBackgroundImage:[UIImage imageNamed:@"Login_enter_icon"] forState:UIControlStateNormal];
        [_enterBtn setTitle:@"确认" forState:UIControlStateNormal];
        [_enterBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction ) forControlEvents:UIControlEventTouchUpInside];
        _enterBtn.titleLabel.font = Bold(20);
    }
    return _enterBtn;
}

-(UITableView *)listView{
    
    if (!_listView) {
        _listView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavHeight + 10, WIDTH, HEIGHT - NavHeight - kSafeAreaHeight - 180 * FMScaleX)];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        _listView.backgroundColor = UIColor.clearColor;
        _listView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_listView registerClass:[MeSetInfoCell class] forCellReuseIdentifier:@"MeSetInfoCell"];
    }
    return _listView;
}

-(BRDatePickerView *)dateView{
    
    if (!_dateView) {
        _dateView =  [[BRDatePickerView alloc]init];
        _dateView.pickerMode = BRDatePickerModeYMD;
        _dateView.title = @"选择年月日";
        // datePickerView.selectValue = @"2019-10-30";
        _dateView.selectDate = [NSDate br_setYear:2019 month:10 day:30];
        _dateView.minDate = [NSDate br_setYear:1929 month:3 day:12];
        _dateView.maxDate = [NSDate date];
        @WeakObj(self)
        _dateView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
            @StrongObj(self)
            UserInfoModel.shareInstance.userModel.birthday = selectValue;
            [self.listView reloadData];
        };
        // 设置自定义样式
        BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
        customStyle.pickerColor = UIColor.whiteColor;
        customStyle.pickerTextColor = RDRGB(0x9A7742);
        customStyle.separatorColor = RDRGB(0x9A7742);
        _dateView.pickerStyle = customStyle;
        }
    return _dateView;
}

-(BRStringPickerView *)sexView{
    
    if (!_sexView) {
        _sexView = [[BRStringPickerView alloc]init];
        _sexView.pickerMode = BRStringPickerComponentSingle;
        _sexView.title = @"选择性别";
        _sexView.dataSourceArr = @[@"男",@"女"];
        @WeakObj(self)
        _sexView.resultModelBlock = ^(BRResultModel * _Nullable resultModel) {
            @StrongObj(self)
            NSInteger sex = [resultModel.value isEqualToString:@"女"] ? 2 : 1;
            UserInfoModel.shareInstance.userModel.sex = sex;
            [self.listView reloadData];
        };
        // 设置自定义样式
        BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
        customStyle.pickerColor = UIColor.whiteColor;
        customStyle.pickerTextColor = RDRGB(0x9A7742);
        customStyle.separatorColor = RDRGB(0x9A7742);
        _sexView.pickerStyle = customStyle;
    }
    return _sexView;
}

-(MeSetNameView *)setNameView{
    
    if (!_setNameView) {
        _setNameView = [[MeSetNameView alloc]initWithFrame:self.view.bounds];
        @WeakObj(self)
        _setNameView.enterdBtnBlock = ^(NSString * _Nonnull name, NSInteger type) {
            @StrongObj(self)
            switch (type) {
                case 1:
                    UserInfoModel.shareInstance.userModel.nickName = name;
                    break;
                case 2:
                    UserInfoModel.shareInstance.userModel.height = name;

                    break;
                case 3:
                    UserInfoModel.shareInstance.userModel.weight = name;
                    break;
                default:
                    break;
            }
            [self.listView reloadData];
        };
        [self.view addSubview:_setNameView];
    }
    return _setNameView ;
}


@end
